package training;

import java.util.*;

/**
 * @Author liangzai
 * @Description:
 */
public class J25_8_8_Training {
    //谁进线下了？II
    public static void main1(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] nums = new int[21];
        nums[1] = 25; nums[2] = 21; nums[3] = 18;nums[4] = 16;nums[5] = 15;nums[6] = 14;nums[7] = 13;nums[8] = 12;
        nums[9] = 11;nums[10] = 10;nums[11] = 9;nums[12] = 8;nums[13] = 7;nums[14] = 6;nums[15] = 5;nums[16] = 4;
        nums[17] = 3;nums[18] = 2;nums[19] = 1;
        boolean[] flag = new boolean[31];
        System.out.println(flag[1]);
        int[][] team = new int[31][2];
        for (int i = 0; i < 31; i++) {
            team[i][0] = i;
        }
        int n = scanner.nextInt();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 20; j++) {
                int c = scanner.nextInt();
                int p = scanner.nextInt();
                team[c][1] += nums[p];
                flag[c] = true;
            }
        }
        team[0][1] = Integer.MAX_VALUE;
        Arrays.sort(team,(a,b)->{
            return b[1] - a[1];
        });
        for (int i = 1; i < 31; i++) {
            if(flag[i]) {
                System.out.println(team[i][0] + " " + team[i][1]);
            }
        }
    }

    //势均力敌

    class K implements Comparable<K> {
        long k;
        int v;

        public K(long k, int v) {
            this.k = k;
            this.v = v;
        }

        @Override
        public int compareTo(K o) {
            if (this.k != o.k) {
                return Long.compare(this.k, o.k);
            }
            return Integer.compare(this.v, o.v);
        }
    }

    public class Main {
        public void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = sc.nextInt();
            }
            Arrays.sort(arr);

            long sum = 0;
            List<Integer> vals = new ArrayList<>();
            do {
                int w = 0;
                for (int v : arr) {
                    w = w * 10 + v;
                }
                vals.add(w);
                sum += (long) w * w;
            } while (nextPermutation(arr));

            if (sum % 2 == 1) {
                return;
            }

            int cnt = vals.size() / 2;
            int mask = 1 << cnt;
            TreeMap<K, Integer> hp = new TreeMap<>();

            for (int i = 0; i < mask; i++) {
                long tsum = 0;
                int z = 0;
                for (int j = 0; j < cnt; j++) {
                    if ((i & (1 << j)) != 0) {
                        int val = vals.get(j + cnt);
                        tsum += (long) val * val;
                        z++;
                    }
                }
                hp.put(new K(tsum, z), i);
            }

            int ans1 = -1, ans2 = -1;
            for (int i = 0; i < mask; i++) {
                long tsum = 0;
                int z = 0;
                for (int j = 0; j < cnt; j++) {
                    if ((i & (1 << j)) != 0) {
                        int val = vals.get(j);
                        tsum += (long) val * val;
                        z++;
                    }
                }
                K target = new K(sum / 2 - tsum, cnt - z);
                if (hp.containsKey(target)) {
                    ans1 = i;
                    ans2 = hp.get(target);
                    break;
                }
            }

            for (int i = 0; i < cnt; i++) {
                if ((ans1 & (1 << i)) != 0) {
                    System.out.println(vals.get(i));
                }
            }
            for (int i = 0; i < cnt; i++) {
                if ((ans2 & (1 << i)) != 0) {
                    System.out.println(vals.get(i + cnt));
                }
            }
        }

        // 实现类似C++ next_permutation的功能
        private static boolean nextPermutation(int[] arr) {
            int i = arr.length - 2;
            while (i >= 0 && arr[i] >= arr[i + 1]) {
                i--;
            }
            if (i < 0) {
                return false;
            }
            int j = arr.length - 1;
            while (arr[j] <= arr[i]) {
                j--;
            }
            // 交换元素
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            // 反转后缀
            reverse(arr, i + 1, arr.length - 1);
            return true;
        }

        // 反转数组指定范围
        private static void reverse(int[] arr, int start, int end) {
            while (start < end) {
                int temp = arr[start];
                arr[start] = arr[end];
                arr[end] = temp;
                start++;
                end--;
            }
        }
    }

}






